package com.angel.controller;

import com.angel.exception.ErrorInfo;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 异常处理控制器
 * @author Rabit
 */
@Controller
public class ExceptionController {

    @RequestMapping(value = "/error/{code}")
    public ModelAndView notFindPage(@PathVariable int code) {
        String pager = "notfindpage";
        Map<String,Object> errMap =new HashedMap();
        switch (code) {
            case 401:
                errMap.put("code",401);
                break;
            case 404:
                errMap.put("code",404);
                break;
            case 500:
                errMap.put("code",500);
             //   pager = "/content/error-pager";
                break;
                default:
        }
        ModelAndView mv=new ModelAndView(pager);
        mv.addAllObjects(errMap);
        return mv;
    }

    @RequestMapping(value = "/status/{code}")
    @ResponseBody
    public Object errorStatus(@PathVariable int code) {
        ErrorInfo<String> errorInfo = new ErrorInfo();
        switch (code) {
            case 401:
                errorInfo.setCode(401);
                errorInfo.setMessage("没有权限访问该路径");
                break;
            case 404:
                errorInfo.setCode(404);
                errorInfo.setMessage("改");
                break;
            case 500:
                errorInfo.setCode(500);
                errorInfo.setMessage("服务器内部错误");
                break;
            default:
                errorInfo.setCode(ErrorInfo.ERROR);
                errorInfo.setMessage("其他错误");
        }

        return errorInfo;
    }
}
